import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:get/get.dart';

import '../../../../bean/bean_category.dart';
import '../../../../bean/bean_integral.dart';
import '../../../../bean/bean_user_account.dart';
import 'account_center_details_controller.dart';

class AccountTaskDetailsController extends GetxController {
  int taskId;

  AccountTaskDetailsController(this.taskId);

  List<IntegralStatBean> waterList = [];
  bool initLoading = true;
  bool waterLoading = false;

  Future<bool> requestWaterList({bool click = false}) async {
    if (click) {
      waterLoading = true;
      update(["accountWater"]);
    }
    Map<String, dynamic> monthMap = {
      "taskId": taskId,
      "page": 1,
      "limit": 100,
    };
    //答题数据
    ResponseInfo statisRresponseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.integralStatisticsRecoderDetailsTask,
      queryParameters: monthMap,
    );
    waterLoading = false;
    initLoading = false;
    if (statisRresponseInfo.success) {
      var responseData = statisRresponseInfo.data;
      if (statisRresponseInfo.success) {
        var responseData = statisRresponseInfo.data;
        if (responseData != null) {
          List list = responseData['list'];
          List<IntegralStatBean> newList = [];
          for (int i = 0; i < list.length; i++) {
            IntegralStatBean _rankStatBean = IntegralStatBean.fromJson(list[i]);
            newList.add(_rankStatBean);
          }
          waterList = newList;
        }
      }
    }

    update(["accountWater"]);
    return true;
  }

  ///------------------------------------任务详情 start ------------------------------------------------------
  //任务中的题分类
  List<CategoryBean> studyCategoryList = [];

  //任务详情
  TaskBean taskBean;

  Future<bool> requestTaskInfo({bool click = false}) async {
    if (click) {
      initLoading = true;
      update(["taskList"]);
    }
    UserBean userBean = UserHelper.getInstance.userBean;
    int userId = userBean.userId;
    Map<String, dynamic> map2 = {};
    map2["mineId"] = userBean.mineId;
    map2["userId"] = userBean.userId;

    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
        url: HttpHelper.taskDetails + "/$taskId", queryParameters: map2);
    initLoading = false;
    studyCategoryList = [];
    if (responseInfo.success) {
      //解析数据
      dynamic data = responseInfo.data;
      taskBean = TaskBean.fromMap(data);
      studyCategoryList = taskBean.appStudyCategoryList;
    }
    update(["taskList"]);
    return true;
  }

  ///------------------------------------任务详情 end ------------------------------------------------------
  ///------------------------------------任务记录start ------------------------------------------------------
  int totalTaskCount = 0;
  int totalTaskPage = 0;

  List<TaskBeanDetails> taskDetailsList = [];
  bool taskDetailsLoading = false;

  Future<bool> requestTaskDetailsList({bool click = false}) async {
    UserBean userBean = UserHelper.getInstance.userBean;
    Map<String, dynamic> map2 = {};
    map2["page"] = 1;
    map2["limit"] = 30;
    map2["taskId"] = taskId;
    int userId = userBean.userId;

    if (click) {
      taskDetailsLoading = true;
      update(["taskDetailsList"]);
    }

    ResponseInfo responseInfo = await DioUtils.instance
        .getRequest(url: HttpHelper.taskDetailsPage, queryParameters: map2);
    taskDetailsList = [];
    taskDetailsLoading = false;
    if (responseInfo.success) {
      //解析数据
      dynamic data = responseInfo.data;
      totalTaskCount = data["totalCount"] ?? 0;
      totalTaskPage = data["totalPage"] ?? 0;
      dynamic list = data["list"];
      if (list != null) {
        for (dynamic item in list) {
          TaskBeanDetails bean = TaskBeanDetails.fromMap(item);
          taskDetailsList.add(bean);
        }
      }
    }
    update(["taskDetailsList"]);
    return true;
  }

  bool isLoading = false;

  Future<bool> onRefresh({bool click = false}) async {
    if (click) {
      isLoading = true;
      update(["taskList"]);
    }
    if (Get.isRegistered<AccountCenterDetailsController>()) {
      await Get.find<AccountCenterDetailsController>().requestUserAccount();
    }
    await requestWaterList();
    await requestTaskInfo();
    await requestTaskDetailsList();

    isLoading = false;
    update(["taskList"]);
    return true;
  }

  @override
  void onReady() {
    super.onReady();
    if (Get.isRegistered<AccountCenterDetailsController>()) {
      Get.find<AccountCenterDetailsController>().requestUserAccount();
    }
    requestWaterList();
    requestTaskInfo();
    requestTaskDetailsList();
  }
}
